package com.activiti.z_six.controller;

import com.activiti.z_six.aspectj.annotation.OperLog;
import com.activiti.z_six.aspectj.operate.LogConst;
import com.activiti.z_six.entity.controllerParams.QueryParamDto;
import com.activiti.z_six.dto.OutTotalDto;
import com.activiti.z_six.entity.formComponents.FormEntity;
import com.activiti.z_six.service.FormMapService;
import com.activiti.z_six.service.FormValueService;
import com.activiti.z_six.util.ResultRes;
import com.activiti.z_six.common.Constants;
import com.activiti.z_six.util.SystemConfig;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("/formData")
public class FormValueController {
    @Autowired
    private FormValueService formValueService;

    @Autowired
    private FormMapService formMapService;

    /**
     * 表数据添加", notes = "表数据添加
     * @param id 数据表id
     */
    @OperLog(operModul = "表单操作" , operType = LogConst.OTHER , operDesc = "根据表单ID创建表")
    @PostMapping({"/getData"})
    public void add(@RequestBody String id) throws Exception {
        FormEntity formEntity=formMapService.getFormEntity(id);
        if(null !=formEntity) {
            formValueService.saveFormValue(id,JSONObject.parseObject(formEntity.getForm_json()));
        }
    }

    /**
     * 查询表单数据集
     */
    @OperLog(operModul = "表单操作" , operType = LogConst.OTHER , operDesc = "根据表单id获取数据列表")
    @PostMapping({"/getDataById"})
    public ResultRes getFormData(@RequestBody QueryParamDto paramDto, HttpServletRequest request) throws Exception {
//        Assert.isTrue(paramDto==null|| StringUtils.isEmpty(paramDto.getId()),"请求参数非法");
        /**
         * 根据id获取到具体表单
         */
        int startIndex = (paramDto.getPageno() - 1) * paramDto.getPagesize();
        int maxIndex = startIndex + paramDto.getPagesize();
        FormEntity formEntity = formMapService.getFormEntity(paramDto.getId());
        //获取到具体的表单名称和对应的json
        if (null != formEntity) {
            if (StringUtils.isEmpty(paramDto.getType()) || Constants.ONE.equals(paramDto.getType())) {
                OutTotalDto list = formValueService.selectFormData(formEntity.getForm_data_table(), paramDto, startIndex, paramDto.getPagesize());
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("list", list.getList());
                hashMap.put("total", list.getTotal());
                return ResultRes.success(hashMap);
            } else {
                return ResultRes.error("不存在id对象。");
            }
        }
        return null;
    }


    /**
     * 保存表单数据
     * @param param
     * @return
     */
    @OperLog(operModul = "表单操作" , operType = LogConst.UPDATE , operDesc = "保存表单数据")
    @PostMapping("/saveFormDataValue")
    public ResultRes saveFormDataValue(@RequestBody JSONObject param) throws Exception {
        String id=param.getString("id");
        String mapJson=param.getString("mapJson");
        String genId = param.getString("genId");
        String dataJson=param.getString("dataJson");
        String msg=formMapService.saveFormDataJson(id,mapJson,dataJson);
        if(msg=="error"){
            return ResultRes.error(msg);
        }
        else{
            genId=msg;
            try {
                formValueService.saveFormValueByJson(genId, mapJson,dataJson);
                return ResultRes.success(msg);
            }
            catch (Exception ex){
                return ResultRes.error(ex.getMessage());
            }

        }
    }
}
